package com.dd.admin.business.stock.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.stockDetail.domain.StockDetailDto;
import com.dd.admin.business.stockDetail.domain.StockDetailVo;
import com.dd.admin.business.stockDetail.entity.StockDetail;
import com.dd.admin.business.stockDetail.service.StockDetailService;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.stock.entity.Stock;
import com.dd.admin.business.stock.domain.StockVo;
import com.dd.admin.business.stock.domain.StockDto;
import com.dd.admin.business.stock.service.StockService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 库存单 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-27
 */
@Api(tags = "库存单")
@RestController
public class StockController {

    @Autowired
    StockService stockService;
    @Autowired
    StockDetailService stockDetailService;

    @ApiOperation(value = "库存单-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/stock/page")
    public ResultBean<IPage<StockVo>> page(StockDto stockDto) {
        stockDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<StockVo> pageInfo = stockService.selectStockPage(stockDto);
        List<StockVo> list = pageInfo.getRecords();
        list.stream().forEach(stockVo -> {
            StockDetailDto stockDetailDto = new StockDetailDto();
            stockDetailDto.setStockId(stockVo.getStockId());
            List<StockDetailVo> stockDetailVos = stockDetailService.selectStockDetailList(stockDetailDto);
            stockVo.setStockDetailVos(stockDetailVos);
        });
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "库存单-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/stock/list")
    public ResultBean<List<StockVo>> list(StockDto stockDto) {
        List<StockVo> list = stockService.selectStockList(stockDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "库存单-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/stock/add")
    public ResultBean<Stock> add(@RequestBody @Validated StockDto stockDto) {
        stockService.addStockIn(stockDto);
        return ResultBean.success();
    }

    @ApiOperation(value = "库存单-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/stock/addStockOut")
    public ResultBean<Stock> addStockOut(@RequestBody @Validated StockDto stockDto) {
        stockService.addStockOut(stockDto);
        return ResultBean.success();
    }

    @ApiOperation(value = "库存单-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/stock/{stockId}")
    public ResultBean<StockVo> get(@PathVariable @NotBlank String stockId) {
        Stock stock = stockService.getById(stockId);
        StockVo stockVo = BeanUtil.copyProperties(stock,StockVo.class);
        return ResultBean.success(stockVo);
    }

    @ApiOperation(value = "库存单-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/stock/update")
    public ResultBean<Stock> update(@RequestBody @Validated(UpdateGroup.class) StockDto stockDto) {
        Stock stock = BeanUtil.copyProperties(stockDto, Stock.class);
        stockService.updateById(stock);
        return ResultBean.success(stock);
    }

    @ApiOperation(value = "库存单-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/stock/delete/{stockId}")
    public ResultBean<Stock> delete(@PathVariable @NotBlank String stockId) {
        Boolean b = stockService.removeById(stockId);
        return ResultBean.success(b);
    }
}
